/*
 * Created on 2006-7-20
 *
  */
package com.carsite.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author shaojie.ji
 */
public class QueryMap {
	
	private Map  sqlMap = new HashMap();
	
	private Map  dateMap = new HashMap();
	
	private Map  dataMap = new HashMap();
	
	private int page = 0;
	
	private int size = 0;
	
	private String orderStr;
	
	
	
	public String getOrderStr() {
		return orderStr;
	}
	public void setOrderStr(String orderStr) {
		this.orderStr = orderStr;
	}
	/**
	 * @return Returns the page.
	 */
	public int getPage() {
		return page;
	}
	/**
	 * @param page The page to set.
	 */
	public void setPage(int page) {
		this.page = page;
	}
	/**
	 * @return Returns the size.
	 */
	public int getSize() {
		return size;
	}
	/**
	 * @param size The size to set.
	 */
	public void setSize(int size) {
		this.size = size;
	}
	public void put(String key,String value){
		if(sqlMap.containsKey(key)){
			sqlMap.remove(key);
		}
		if (value != null && !"".equals(value)) {
		   sqlMap.put(key,value);		   
		}
	}
	
	
	public void putData(String key,String value){
		if(dataMap.containsKey(key)){
			dataMap.remove(key);
		}
		if (value != null && !"".equals(value)) {
			dataMap.put(key,value);		   
		}
	}
	
	public void putDatePeriod(String key,String beginDateStr,String endDateStr) throws ParseException{
		
		String fmtStr = "yyyy-MM-dd";  
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmtStr);
		
		dateMap.put("keyName",key);
		
		if (beginDateStr != null && !"".equals(beginDateStr)) {
			
	        Date beginDate = simpleDateFormat.parse(beginDateStr);
            
	        dateMap.put("beginDate",beginDate);

		}
		if (endDateStr != null && !"".equals(endDateStr)) {
					
	        Date endDate = simpleDateFormat.parse(endDateStr);
            
	        dateMap.put("endDate",endDate);
         }
	}

	
	public void putDatePeriod(String key,Date beginDate,Date endDate){
		
		dateMap.put("keyName",key);
		
		if (beginDate != null ) {
		    
	        dateMap.put("beginDate",beginDate);

		}
		if (endDate != null ) {
		    
	        dateMap.put("endDate",endDate);
         }
	}
	
	
	public Map getDateMap(){
	     return 	dateMap;
	}
	
	public Map getDataMap(){
	     return 	dataMap;
	}
	
	public String getSingleTableSql(){
			String sql = "where 1=1 ";
			if(sqlMap.size() > 0){
			  	 Iterator itr = sqlMap.keySet().iterator();
			     
			  	 for (int i = 0; i < sqlMap.size();i++ ) {
					    
						String key = (String) itr.next();
						String value = (String) sqlMap.get(key);
						sql = sql +" and " +key +"= '" +value+"'";
				
				 }	
			}
			return sql;
	}
	
	public void addCondition(String column,String operator,String value){

		QueryCodition queryCondition =  new  QueryCodition();
		queryCondition.setColumn(column);
		queryCondition.setOperator(operator);
		queryCondition.setValue(value);
		if (value != null && !"".equals(value)) {
			sqlMap.put(column+operator,queryCondition);	
		}
	}
	
	public String getQuerySql(){
		String sql = "where 1=1 ";
		if(sqlMap.size() > 0){
		  	 Iterator itr = sqlMap.keySet().iterator();
		     
		  	 for (int i = 0; i < sqlMap.size();i++ ) {
				    
					String key = (String) itr.next();
					
					QueryCodition queryCondition = (QueryCodition) sqlMap.get(key);
					
					if("like".equals(queryCondition.getOperator())){
						sql = sql +" and " +queryCondition.getColumn() +" " +queryCondition.getOperator()+ "'%" +queryCondition.getValue()+"%'";
					}else if("in".equals(queryCondition.getOperator())){
						 sql = sql +" and " +queryCondition.getColumn() +" " +queryCondition.getOperator()+  queryCondition.getValue();
					}else if("join".equals(queryCondition.getOperator())){
						sql = sql +" and " +queryCondition.getColumn() +"="+  queryCondition.getValue();
					}else if("is".equals(queryCondition.getOperator())){
						sql = sql +" and " +queryCondition.getColumn() +" is "+  queryCondition.getValue();
					}else{
					  sql = sql +" and " +queryCondition.getColumn() +" " +queryCondition.getOperator()+ "'" +queryCondition.getValue()+"'";
					} 
			}	 
		}
		return sql;
    }
	
	class  QueryCodition{
	    private String column;
	    private String operator;
	    private String value;
	    
	
		/**
		 * @return Returns the column.
		 */
		public String getColumn() {
			return column;
		}
		/**
		 * @param column The column to set.
		 */
		public void setColumn(String column) {
			this.column = column;
		}
		/**
		 * @return Returns the operator.
		 */
		public String getOperator() {
			return operator;
		}
		/**
		 * @param operator The operator to set.
		 */
		public void setOperator(String operator) {
			this.operator = operator;
		}
		/**
		 * @return Returns the value.
		 */
		public String getValue() {
			return value;
		}
		/**
		 * @param value The value to set.
		 */
		public void setValue(String value) {
			this.value = value;
		}
	}

}